package com.qianfeng.missfood.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.adapter.Category_Bottomgridview_adapter;
import com.qianfeng.missfood.adapter.Category_Topgridview_adapter;
import com.qianfeng.missfood.category_bean.Category_Bean;
import com.qianfeng.missfood.category_bean.Category_Info;
import com.qianfeng.missfood.category_bean.Category_Second_Info;
import com.qianfeng.missfood.constants.UrlConstants;
import com.qianfeng.missfood.ui.CategoryDetailsActivity;
import com.qianfeng.missfood.utils.CategoryGridView;

import org.simple.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class Category_GridView_Fragment extends Fragment implements AdapterView.OnItemClickListener {
    @ViewInject(R.id.category_top_textview)
    private TextView category_top_textview;
    @ViewInject(R.id.category_bottom_textview)
    private TextView category_bottom_textview;
    @ViewInject(R.id.category_top_gridview)
    private CategoryGridView category_top_gridview;
    @ViewInject(R.id.category_bottom_gridview)
    private CategoryGridView category_bottom_gridview;
    private HttpUtils httpUtils = new HttpUtils();
    private ArrayList<Category_Info> topInfos = new ArrayList<>();
    private ArrayList<Category_Second_Info> bottomInfos = new ArrayList<>();
    private Category_Topgridview_adapter top_gridview_adapter;
    private Category_Bottomgridview_adapter bottomgridview_adapter;
    private ArrayList<String> topCategoryIds = new ArrayList<>();
    private ArrayList<String> bottomCategoryIds=new ArrayList<>();


    public Category_GridView_Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category__grid_view_, container, false);
        ViewUtils.inject(this, view);
        initView(view);
        initData();
        return view;
    }


    private void initView(View view) {
        top_gridview_adapter = new Category_Topgridview_adapter(topInfos, getActivity());
        category_top_gridview.setAdapter(top_gridview_adapter);
        bottomgridview_adapter = new Category_Bottomgridview_adapter(bottomInfos, getActivity());
        category_bottom_gridview.setAdapter(bottomgridview_adapter);

        //给gridview设置适配器
        category_top_gridview.setOnItemClickListener(this);
        category_bottom_gridview.setOnItemClickListener(this);
    }

    private void initData() {
        String CategoryGridViewUrl = UrlConstants.categoryGridViewUrl;
        httpUtils.send(HttpRequest.HttpMethod.GET, CategoryGridViewUrl, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (responseInfo != null) {
                    String result = responseInfo.result;
                    Category_Bean bean = JSONObject.parseObject(result, Category_Bean.class);
                    if (bean != null && bean.getCategoryBOs() != null) {
                        //gridview的上半部分
                        category_top_textview.setText(bean.getCategoryBOs().get(getArguments().getInt("position")).getDesc());
                        if (bean.getCategoryBOs().get(getArguments().getInt("position")).getCategories() == null) {
                            //上门吃情况
                            Category_Info last_Info = new Category_Info();
                            last_Info.setName(bean.getCategoryBOs().get(getArguments().getInt("position")).getName());
                            last_Info.setIcon(bean.getCategoryBOs().get(getArguments().getInt("position")).getIcon());
                            last_Info.setId(bean.getCategoryBOs().get(getArguments().getInt("position")).getId());
                            topInfos.add(last_Info);
                            //获得点击事件传递的categoryIds(上门吃情况)
                            topCategoryIds.add(bean.getCategoryBOs().get(getArguments().getInt("position")).getId());
                        } else if (bean.getCategoryBOs().get(getArguments().getInt("position")).getCategories() != null) {
                            topInfos.addAll(bean.getCategoryBOs().get(getArguments().getInt("position")).getCategories());
                            //获得点击事件传递的categoryIds(gridview上半部分)
                            for (Category_Info info : bean.getCategoryBOs().get(getArguments().getInt("position")).getCategories()) {
                                topCategoryIds.add(info.getId());
                            }

                            //gridview的下半部分
                            for (Category_Info info : bean.getCategoryBOs().get(getArguments().getInt("position")).getCategories()) {
                                if (info.getCategories() != null) {
                                    category_bottom_textview.setText(info.getName());
                                    bottomInfos.addAll(info.getCategories());
                                    bottomgridview_adapter.notifyDataSetChanged();
                                    //获得点击事件传递的categoryIds(gridview下半部分)
                                    for (Category_Second_Info second_info:bottomInfos) {
                                        bottomCategoryIds.add(second_info.getId());
                                    }
                                }
                            }
                        }

                        top_gridview_adapter.notifyDataSetChanged();

                    }
                }


            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }

    /**
     * gridview的item点击事件(上下两个gridview)
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.category_top_gridview:
                Intent topIntent = new Intent(getActivity(), CategoryDetailsActivity.class);
                topIntent.putExtra("categoryId", topCategoryIds.get(position));
                topIntent.putExtra("Infos", topInfos);
                topIntent.putExtra("CategoryIds",topCategoryIds);
                topIntent.putExtra("position",position);
                //设置标记,好让下一步判断点击的是第一个还是第二个gridview
                topIntent.putExtra("Tag","Top");
                startActivity(topIntent);
                break;

            case R.id.category_bottom_gridview:
                Intent bottomIntent = new Intent(getActivity(), CategoryDetailsActivity.class);
                bottomIntent.putExtra("categoryId", bottomCategoryIds.get(position));
                bottomIntent.putExtra("Infos", bottomInfos);
                bottomIntent.putExtra("CategoryIds",bottomCategoryIds);
                bottomIntent.putExtra("position",position);
                //设置标记,好让下一步判断点击的是第一个还是第二个gridview
                bottomIntent.putExtra("Tag","Bottom");
                startActivity(bottomIntent);
                break;
        }

    }
}
